package com.andy.customerService.utils;


import com.andy.customerService.entity.JWTUserInfo;

import java.util.UUID;

public class UserLocalUtil {
    private UserLocalUtil() {
        throw new IllegalStateException("Utility class");
    }

    private static ThreadLocal<JWTUserInfo> authUserThreadLocal = new InheritableThreadLocal<>();

    public static void setAuthUser(JWTUserInfo user) {
        authUserThreadLocal.set(user);
    }

    public static JWTUserInfo getAuthUser() {
        return authUserThreadLocal.get();
    }

    public static Integer getAuthUserId() {
        JWTUserInfo user = UserLocalUtil.getAuthUser();
        return user == null ? null : user.getId();
    }

    public static void clear() {
        authUserThreadLocal.remove();
    }

    public static String getSessionId() {
        return UUID.randomUUID().toString().replace("-", "");
    }

}
